<template>
  <div style="padding:20px">
    <el-form ref="form" label-width="80px">
      <el-form-item label="讲师姓名">
        <el-input v-model="teacher.name"/>
      </el-form-item>
      <el-form-item label="讲师简介">
        <el-input v-model="teacher.intro"/>
      </el-form-item>
      <el-form-item label="讲师资历">
        <el-input v-model="teacher.career"/>
      </el-form-item>
      <el-form-item label="讲师头衔">
        <el-select v-model="teacher.level">
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item>

      <el-form-item label="讲师排序">
        <el-input-number v-model="teacher.sort" :min="1"/>
      </el-form-item>

      <el-form-item label="入驻时间">
        <el-date-picker
          v-model="teacher.joinDate"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"/>
      </el-form-item>
      <!-- TODO:讲师头像 -->
      <el-form-item label="讲师头像">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="uploadUrl"
          class="avatar-uploader">
          <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="flag" type="primary" @click="handleSaveOrUpdateTeacher">提交</el-button>
        <el-button @click="clear">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher'
export default {
  data() {
    return {
      teacher: {
        sort: 1
      },
      uploadUrl: 'http://localhost:8120/admin/oss/file/upload?module=avatar',
      flag: false
    }
  },
  created() {
    // this.$route.query.k 代表访问当前路由的查询参数 url?k=v
    // this.$route.params.tid 代表访问当前路由的路径参数
    // 根据id查询讲师数据回显
    if (this.$route.params.tid) {
      this.getTeacher()
    }
  },
  methods: {
    getTeacher() {
      teacherApi.getById(this.$route.params.tid).then(r => {
        this.teacher = r.data.item
      })
    },
    // 提交按钮的单击事件
    handleSaveOrUpdateTeacher() {
      this.flag = true // 防止表单重复提交，禁用按钮
      if (this.$route.params.tid) {
        // 更新
        this.handleUpdateTeacher()
      } else {
        // 新增
        this.handleSaveTeacher()
      }
    },
    handleSaveTeacher() {
      teacherApi.save(this.teacher).then(r => {
        this.$message.success('新增讲师成功')
        this.flag = false
        // 跳转到讲师列表页面
        //  this 代表vue对象 , this.$router 代表设置给vue对象的 路由对象(router/index.js包含所有路由的配置)
        // this.$route 代表访问当前页面的路由对象，就是页面在router/index.js中映射当前页面的路由配置
        this.$router.push('/teacher/list')
      })
    },
    handleUpdateTeacher() {
      teacherApi.update(this.teacher).then(r => {
        this.$message.success('更新讲师成功')
        this.flag = false
        this.$router.push('/teacher/list')
      })
    },
    handleAvatarSuccess(res, file) {
      // console.log(res)
      // this.imageUrl = URL.createObjectURL(file.raw)
      if (res.code === 20000) {
        this.teacher.avatar = res.data.path
        this.$forceUpdate()
      } else {
        this.$message.error(res.message)
      }
      // this.teacher.avatar = res.data.path
      // //  强制vue重新渲染
      // this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    clear() {
      this.teacher = {}
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
